import tools.utils as tu
from flask import render_template, redirect, request
from . import employee


@employee.route('/', methods=['GET', 'POST'])
def index():
    """
    首页
    :return:
    """
    return """
                <div>
                    <h3>路由列表</h3>
                    <table cellpadding="0" cellspacing="0" border="1">
                        <thead>
                            <tr>
                                <th>路由</th>
                                <th>信息</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>/employee/employees</td>
                                <td>查询员工信息</td>
                            <tr>
                            <tr>
                                <td>/employee/insert</td>
                                <td>添加员工信息</td>
                            <tr>
                            <tr>
                                <td>/employee/delete/</td>
                                <td>删除查询员工信息</td>
                            <tr>
                            <tr>
                                <td>/employee/update/</td>
                                <td>修改员工信息</td>
                            <tr>
                        </tbody>
                    </table>
                </div>
           """


@employee.route('/employees', methods=['GET', 'POST'])
def get_employees():
    """
    查询所有员工信息
    :return:
    """
    employee_list = tu.reset()
    return render_template('getEmployees.html', employee_list=employee_list)


@employee.route('/insert', methods=['GET', 'POST'])
def add_employee():
    """
    添加员工信息页面和添加员工信息功能
    :return:
    """
    if request.method == 'GET':
        return render_template('addEmployees.html')
    else:
        # 获取用户输入的内容
        uname = request.form.get('uname')
        upwd = request.form.get('upwd')
        truename = request.form.get('truename')
        birth = request.form.get('birth')
        salary = request.form.get('salary')
        mobile = request.form.get('mobile')
        is_success = tu.insert_employee(uname, upwd, truename, birth, salary, mobile)
        if is_success:
            # 添加成功后刷新数据
            # employee_list = tu.reset()
            # return render_template('getEmployees.html', employee_list=employee_list)
            return "<script>alert('添加员工信息成功！');window.location.href = '/employee/employees'</script>"
        else:
            return "<script>alert('添加员工信息失败，请重新添加！');window.location.href = '/employee/insert'</script>"


@employee.route('/delete/<uid>', methods=['GET', 'POST'])
def delete_employee(uid):
    """
    删除员工信息
    :param uid:
    :return:
    """
    is_success = tu.delete_employee(uid)
    if is_success:
        # 删除成功后刷新数据
        # employee_list = reset()
        return "<script>alert('删除员工信息成功！');window.location.href = '/employee/employees'</script>"
    else:
        return "<script>alert('删除员工信息失败！');window.location.href = '/employee/employees'</script>"


@employee.route('/update/<uid>', methods=['GET', 'POST'])
def update_employee(uid):
    """
    修改员工信息页面和修改员工信息功能
    :param uid:
    :return:
    """
    if request.method == 'GET':
        employee = tu.select_employee_first(uid)
        return render_template('updateEmployees.html', employee=employee)
    else:
        # 获取用户输入的内容
        uid = request.form.get('uid')
        uname = request.form.get('uname')
        upwd = request.form.get('upwd')
        truename = request.form.get('truename')
        birth = request.form.get('birth')
        salary = request.form.get('salary')
        mobile = request.form.get('mobile')
        is_success = tu.update_employee(uid, uname, upwd, truename, birth, salary, mobile)
        if is_success:
            return "<script>alert('修改员工信息成功！');window.location.href = '/employee/employees'</script>"
        else:
            return "<script>alert('修改员工信息失败！');window.location.href = '/employee/update/<uid>'</script>"








